<?php
/**
 * CK框架 配置类 用于读取配置文件 以及保存配置信息
 * @author wujibing<283109896@qq.com>
 * @version   1.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
final class config
{
    /**
     * config对象
     * @var obejct
     */
    public static $configObject;
    /**
     * 载入配置文件
     * 配置文件允许多次加载
     * @param string $configFile
     * @throws notFoudConfigException 配置文件不存在异常
     */
    public static function loadConfig($configFile)
    {
        $configContent = '';
        try
        {
            file::read($configFile, $configContent);
        } catch (notFoundFile_Exception $e)
        {
            throw new base_Exception(lang::getLang('__NOT__FOUND__CONFIG_FILE__',array($configFile)));
        }

        if(!self::$configObject = json_decode($configContent))
        {
            throw new base_Exception(lang::getLang('__DAMAGE_IN_THE_CONFIG_FILE__',array($configFile)));
        }
    }
    public static function check()
    {
        $checkArray = array(
                'TIME_ZONE' => 'Asia/Chongqing',
                'ACTION_NAME' => 'action',
                'METHOD_NAME' => 'method',
                'DEFAULT_ACTION' => 'index',
                'DEFAULT_METHOD' => 'index',
        		'CK_DEVELOP_NOW' => false,
        		'APP_404_PAGE' => '~404.php',
        		'APP_MESSAGE_PAGE' => '~msg.php'
                );
        foreach($checkArray as $key => $value)
        {
            if(!self::$configObject->$key)
            {
                self::$configObject->$key = $value;
            }
        }
        //判断404 和消息文件存在不
        $showDocumentPage = CK_APPLICATION_DATA_DIR.self::$configObject->APP_404_PAGE;
        if(!file::exists($showDocumentPage))
        {
        	throw new base_Exception(lang::getLang('__APP_404_PAGE_NOT_FOUND__',array($showDocumentPage)));
        }
        $showDocumentPage = CK_APPLICATION_DATA_DIR.self::$configObject->APP_MESSAGE_PAGE;
        if(!file::exists($showDocumentPage))
        {
        	throw new base_Exception(lang::getLang('__APP_MESSAGE_PAGE_NOT_FOUND__',array($showDocumentPage)));
        }
    }
}